import java.util.Scanner;

public class ObjectSystem {
    static Customer customer = new Customer();

    public static void start() {
        System.out.println("请确认身份 1 管理员 2 顾客");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        if (input == 1) {
            Manager.login();
            managerMenu();
        } else if (input == 2) {
            showLoginOptions();
        } else {
            System.out.println("请重新输入身份");
            start();
        }
    }

    static void showLoginOptions() {
        System.out.println("请选择： 1.登录 2.注册");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                customer.login();
                break;
            case 2:
                register();
                break;
            default:
                System.out.println("无效选项，请重新选择");
                showLoginOptions();
                break;
        }
    }

    static void register() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名（至少5个字符）：");
        String username = scanner.next();
        System.out.println("请输入密码（至少8个字符）：");
        String password = scanner.next();
        System.out.println("请输入邮箱：");
        String email = scanner.next();

        Customer newCustomer = new Customer();
        boolean registered = newCustomer.register(username, password, email);

        if (registered) {
            System.out.println("注册成功！");
            customerMenu();
        } else {
            System.out.println("注册失败，请确保用户名和密码符合要求");
            showLoginOptions();
        }
    }

    public static void managerMenu() {
        System.out.println("请选择以下操作");
        System.out.println("1.密码管理");
        System.out.println("2.客户管理");
        System.out.println("3.商品管理");
        System.out.println("4.退出");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        switch (input) {
            case 1:
                Manage.passwordManage();
                break;
            case 2:
                Manage.customerManage();
                break;
            case 3:
                Manage.productManage();
                break;
            case 4:
                start();
            default:
                System.out.println("输入有误，请重新输入");
                managerMenu();
        }
    }

    public static void customerMenu() {
        System.out.println("请选择以下操作");
        System.out.println("1.修改密码");
        System.out.println("2.重置密码");
        System.out.println("3.添加商品到购物车");
        System.out.println("4.从购物车移除商品");
        System.out.println("5.更新购物车商品数量");
        System.out.println("6.结算购物车");
        System.out.println("7.查看购买历史");
        System.out.println("8.退出");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        scanner.nextLine();
        switch (input) {
            case 1:
                System.out.println("请输入新密码");
                String newPassword = scanner.nextLine();
                customer.changePassword(newPassword);
                customerMenu();
                break;
            case 2:
                System.out.println("请输入用户名");
                String username = scanner.nextLine();
                System.out.println("请输入邮箱");
                String email = scanner.nextLine();
                customer.resetPassword(username, email);
                break;
            case 3:
                System.out.println("请输入商品ID");
                String productId = scanner.nextLine();
                System.out.println("请输入数量");
                int quantity = scanner.nextInt();
                customer.addToCart(productId, quantity);
                break;
            case 4:
                System.out.println("请输入商品ID");
                String productIdToRemove = scanner.nextLine();
                customer.removeFromCart(productIdToRemove);
                break;
            case 5:
                System.out.println("请输入商品ID");
                String productIdToUpdate = scanner.nextLine();
                System.out.println("请输入新数量");
                int newQuantity = scanner.nextInt();
                customer.updateProductQuantity(productIdToUpdate, newQuantity);
                break;
            case 6:
                customer.makePayment();
                break;
            case 7:
                customer.viewPurchaseHistory();
                break;
            case 8:
                start();
            default:
                System.out.println("输入有误，请重新输入");
                customerMenu();
        }
    }

}